


Const n4:Int=1024
Const n8:Int=256
Const NUMPATCHES:Int=n4+n8


''EVERY VALUE AS BYTE!
'Int n4
'Int n8
'[Int n12]
'[Int n16]
'
'4 data:
'unsigned char: For x, For y, For ori, writedata
'8 data:



DeleteFile("s2.patch")



SeedRnd MilliSecs()


Global p0:TPixmap, p1:TPixmap, p2:TPixmap, p3:TPixmap, x:Int, y:Int


Global f:String[]=LoadDir("c1")

Global o:TStream=WriteFile("s2.patch")


'''***
WriteInt(o, n4)
WriteInt(o, n8)
WriteInt(o, 0)
WriteInt(o, 0)
'''***


For i=0 To n4-1
  Print (i+1)+"/"+NUMPATCHES

  getpatch(o, 4)
Next

For i=0 To n8-1
  Print (n4+i+1)+"/"+NUMPATCHES

  getpatch(o, 8)
Next



CloseFile(o)

End





Function getpatch(o:TStream, w:Int)
  Local n:Int=Rnd(0, f.length-1)

  Local p:TPixmap=LoadPixmap("c1/"+f[n])

  Local ff:String=StripExt(f[n])
  ff=ff[0..ff.length-1]


  Local en:Double=getenergy(ff, w) / Double((p.width-w-1)*(p.height-w-1))

'100.0
  If en<300.0
    getpatch(o, w)
  Else
    writepatch(ff, o, w)
  EndIf

End Function




Function getenergy:Double(ff:String, w:Int)
  Return getenergy_("c1/"+ff+"0.png", w)+getenergy_("c1/"+ff+"1.png", w)+getenergy_("c1/"+ff+"2.png", w)+getenergy_("c1/"+ff+"3.png", w)
End Function



Function getenergy_:Double(f:String, w:Int)
  Local p:TPixmap=LoadPixmap(f)

  Local x:Int=Rnd(1, p.width-w-1)
  Local y:Int=Rnd(1, p.height-w-1)

  Local en:Double=0.0

  For j=0 To w-1
    For k=0 To w-1
      Local r:Byte=ReadPixel(p, x+j, y+k)

      en=en+r*r
    Next
  Next


  Return en
End Function



Function writepatch(f:String, o:TStream, w:Int)
  Local p0:TPixmap=LoadPixmap("c1/"+f+"0.png")
  Local p1:TPixmap=LoadPixmap("c1/"+f+"1.png")
  Local p2:TPixmap=LoadPixmap("c1/"+f+"2.png")
  Local p3:TPixmap=LoadPixmap("c1/"+f+"3.png")

  Local x:Int=Rnd(1, p0.width-w-1)
  Local y:Int=Rnd(1, p0.height-w-1)

  For k=0 To w-1
    For j=0 To w-1
      ''for ori
      Local r:Byte=ReadPixel(p0, x+j, y+k)
      WriteByte(o, r)
'Print r
      r=ReadPixel(p1, x+j, y+k)
      WriteByte(o, r)

      r=ReadPixel(p2, x+j, y+k)
      WriteByte(o, r)

      r=ReadPixel(p3, x+j, y+k)
      WriteByte(o, r)

    Next
  Next
End Function







